package com.sdy.supermarket.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.mvc.controller.BaseController;
import com.sdy.supermarket.biz.model.GsAppInterface;
import com.sdy.supermarket.biz.model.GsSelfSystem;
import com.sdy.supermarket.biz.service.GsSelfSystemService;
import com.sdy.supermarket.biz.vo.AppUpdateRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自建系统 前端控制器
 * </p>
 *
 * @author zzq
 * @since 2019-07-09
 */
@Slf4j
@RestController
@RequestMapping("/gsSelfSystem")
@SuppressWarnings("unchecked")
public class GsSelfSystemController extends BaseController {
    @Autowired
    private GsSelfSystemService gsSelfSystemService;
    @Autowired
    private SsoService ssoService;

    /**
     * 根据系统id查询详情
     * @param id 系统id
     */
    @GetMapping("/get")
    public Response get(Integer id){
        return Response.success(gsSelfSystemService.getById(id));
    }

    /**
     * 根据系统id查询详情
     * @param code 系统code
     */
    @GetMapping("/getByCode")
    public Response get(String code) throws BizException {
        Assert.isBlank(code, "参数错误！");
        return Response.success(gsSelfSystemService.getByCode(code));
    }

    /**
     * 查询用户自建系统列表
     */
    @GetMapping("/listData")
    public Response listData(HttpServletRequest request){
        LambdaQueryWrapper<GsSelfSystem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GsSelfSystem::getUserId, ssoService.getUserInfo(request).getUserId());
        wrapper.orderByDesc(GsSelfSystem::getDef, GsSelfSystem::getId);
        return Response.success(gsSelfSystemService.list(wrapper));
    }

    /**
     * 查询某个自建系统某个IP下的接口
     */
    @GetMapping("/listApiByCodeAndIp")
    public Response listApiByCodeAndIp(HttpServletRequest request, String code, String ip) {
        List<GsAppInterface> apiAuthList = gsSelfSystemService.listApiByCodeAndIp(code, ip);
        return Response.success(apiAuthList);
    }

    /**
     * 更新自建系统IP地址
     */
    @PostMapping("/update")
    public Response update(HttpServletRequest request, @RequestBody AppUpdateRequest appUpdateRequest)
            throws BizException {
        Assert.isBlank(appUpdateRequest.getCode(), "参数错误");
        Assert.isBlank(appUpdateRequest.getName(), "自建系统名称不能为空");
        appUpdateRequest.setDef(appUpdateRequest.getDef() == null || appUpdateRequest.getDef().equals(0) ? 0 : 1);
        gsSelfSystemService.updateIpAuth(appUpdateRequest, ssoService.getUserInfo(request).getUserId());
        return Response.success();
    }

    /**
     * 新增系统
     */
    @PostMapping("/add")
    public Response add(@RequestBody GsSelfSystem selfSystem, HttpServletRequest request){
        if(selfSystem.hasErrorColumns()){
            return Response.error("参数有误");
        }
        selfSystem.setId(null);
        selfSystem.setCreateTime(new Date());
        selfSystem.setUserId(ssoService.getUserInfo(request).getUserId());
        gsSelfSystemService.createSystem(selfSystem);
        return Response.success();
    }

    /**
     * 删除自建系统
     * @param code 自建系统编码
     */
    @GetMapping("/delete")
    public Response delete(String code, HttpServletRequest request) throws BizException {
        Assert.isBlank(code, "参数有误");
        Integer userId = ssoService.getUserInfo(request).getUserId();
        GsSelfSystem originSystem = gsSelfSystemService.getByCode(code);
        Assert.isNull(originSystem, "自建系统不存在");
        Assert.isTrue(!originSystem.getUserId().equals(userId), "没有修改权限");
        Assert.isTrue(gsSelfSystemService.removeById(originSystem.getId()), "删除失败");
        return Response.success();
    }
}
